package com.zyf.project.capchain.service.impl;

import com.zyf.project.capchain.dao.UserDAO;
import com.zyf.project.capchain.dto.UserDTO;
import com.zyf.project.capchain.exceptions.EmptyParamsException;
import com.zyf.project.capchain.exceptions.SqlErrorException;
import com.zyf.project.capchain.listener.result.impl.Result;
import com.zyf.project.capchain.model.User;
import com.zyf.project.capchain.service.UserService;
import com.zyf.project.capchain.listener.result.template.ResultTemplate;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

/**
 * @author Zhou Yifan
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserDAO userDAO;

    private final ResultTemplate<User> userResultTemplate;

    /**
     * 添加用户
     *
     * @param user 用户对象
     * @return 处理结果
     */
    @Override
    public Result<User> addUser(User user) {
        Result<User> result = new Result<>();
        if (user == null || user.isLogicalEmpty()) {
            throw new EmptyParamsException("user 不存在或者缺少必要参数：uid 和 openid");
        }
        int res = userDAO.insert(user.toDTO());
        if (res < 1) {
            throw new SqlErrorException("数据层执行错误，发生语句为：userDAO.insert(user.toDTO())");
        }
        userResultTemplate.success(result, user);
        return result;
    }

    /**
     * 修改用户
     *
     * @param user 用户对象
     * @return 处理结果
     * @deprecated <p>
     * <b>不建议使用修改</b>
     *     <ol>
     *         <li>一般人员无权修改用户数据</li>
     *         <li>容易导致客户端数据错误</li>
     *     </ol>
     * </p>
     */
    @Override
    @Deprecated
    public Result<User> modifyUser(User user) {
        Result<User> result = new Result<>();
        if (user == null || user.isLogicalEmpty()) {
            throw new EmptyParamsException("user 不存在或者缺少必要参数：uid 和 openid");
        }
        int res = userDAO.updateById(user.toDTO());
        if (res < 1) {
            throw new SqlErrorException("数据层执行错误，发生语句为：userDAO.updateById(user.toDTO())");
        }
        userResultTemplate.success(result, user);
        return result;
    }

    /**
     * 根据 uid 获取用户
     *
     * @param uid 用户id
     * @return 处理结果
     */
    @Override
    public Result<User> getUserByUid(Long uid) {
        Result<User> result = new Result<>();
        if (uid == null || uid < 0) {
            throw new EmptyParamsException("uid 为空或 uid 值异常");
        }
        UserDTO userDTO = userDAO.selectById(uid);
        if (userDTO == null) {
            throw new SqlErrorException("数据层执行错误，发生语句为：userDAO.selectById(uid)");
        }
        userResultTemplate.success(result, userDTO.toModel());
        return result;
    }

    /**
     * 根据 openid 获取用户
     *
     * @param openid 唯一标识符
     * @return 处理结果
     */
    @Override
    public Result<User> getUserByOpenid(@NotNull String openid) {
        Result<User> result = new Result<>();
        if (openid.isEmpty()) {
            throw new EmptyParamsException("openid 为空");
        }
        UserDTO userDTO = userDAO.selectByOpenid(openid);
        if (userDTO == null) {
            throw new SqlErrorException("数据层执行错误，发生语句为：userDAO.selectByOpenid(openid)");
        }
        userResultTemplate.success(result, userDTO.toModel());
        return result;
    }

    /**
     * 删除用户
     *
     * @param user 用户对象
     * @return 处理结果
     * @deprecated <p>
     * <b>不建议使用修改</b>
     *     <ol>
     *         <li>一般人员无权修改用户数据</li>
     *         <li>容易导致客户端数据错误</li>
     *     </ol>
     * </p>
     */
    @Override
    @Deprecated
    public Result<User> deleteUser(User user) {
        Result<User> result = new Result<>();
        if (user == null || user.isLogicalEmpty()) {
            throw new EmptyParamsException("user 不存在或者缺少必要参数：uid 和 openid");
        }
        int res = userDAO.deleteById(user.toDTO());
        if (res < 1) {
            throw new SqlErrorException("数据层执行错误，发生语句为：userDAO.deleteById(user.toDTO())");
        }
        userResultTemplate.success(result, user);
        return result;
    }

}
